package com.mobisys.live.statistic;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mobisys.live.bean.Tencent.StatisticTencentBean;
import com.mobisys.live.response.Response;
import com.mobisys.live.response.tencentcloud.LiveStatResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;


import java.awt.*;
import java.util.*;

import static com.mobisys.live.config.ResponseCode.*;

public class StatisticTencent {

    @Autowired
    private RestTemplate restTemplate = new RestTemplate();
    private ResponseEntity<String> responseEntity ;
    private LiveStatResponse liveStatResponse ;
    private  Response response = new Response();
    private Logger logger = LogManager.getLogger(StatisticTencent.class.getName());


    /*
    *Get_LiveStat：查询指定直播流的推流和播放信息；
    * Get_LivePushStat ：仅返回推流统计信息以提高查询效率；
    * Get_LivePlayStat ：仅返回播放统计信息以提高查询效率。
    *
    * 查询统计信息
    *
    * */
    public Response getStatistic(StatisticTencentBean statisticTencentBean,String interf) throws AWTException {

        //获取相关接口
        statisticTencentBean.setInterfaces(interf);

        String url = "http://statcgi.video.qcloud.com/common_access?"
                + "cmd=" + statisticTencentBean.getCmd()
                + "&interface=" + statisticTencentBean.getInterfaces()
                + "&Param.n.page_no=" + statisticTencentBean.getPage_no()
                + "&Param.n.page_size=" + statisticTencentBean.getPage_size()
                + "&t=" + statisticTencentBean.getT()
                + "&sign=" + statisticTencentBean.getSign()
                + "&Param.s.stream_id=" + statisticTencentBean.getStream_id()
                + "&Param.s.pull_domain=" + statisticTencentBean.getPull_domain();

        logger.debug(" url "+url+"         ");


        if (interf.equals("Get_LiveStat")) {

            System.out.println("++++++++++++++ 3 get statistic ++++++++++++++++++++++++++++++++++++++++++++++");
            /*System.out.println( "延时前:"+new Date().toString()  );

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(   "延时后:"+new Date().toString()   );*/

            responseEntity = restTemplate.getForEntity(url, String.class);
            logger.debug("Get_LiveStat:"+responseEntity.getBody());



           if(responseEntity.getBody().contains("has no stream is living")){
                System.out.println("++++++++++++++ 3.2 statistic is null return err ++++++++++++++++++++++++++++++++++++++++++++++");
                response.setRetureValue(TENCENT_EVENT_ERROR);
                response.setRetureMsg(MSG_TENCENT_EVENT_ERROR);
                return  response;
            }

            System.out.println("++++++++++++++ 3.1 statistic is true ++++++++++++++++++++++++++++++++++++++++++++++");
            liveStatResponse = JSON.parseObject(responseEntity.getBody().toString(),LiveStatResponse.class);
            logger.debug("liveStatResponse  "+liveStatResponse.toString());


            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            logger.debug("Get_LiveStat");
            if(liveStatResponse!=null){
                response.setRetureValue(SUCCESS);
                response.setRetureMsg(MSG_SUCCESS);
                response.setRetureData(liveStatResponse);
            }

        }

        return response;
    }







}
